package impl;

import java.io.DataInputStream;
import java.io.IOException;
import java.net.Socket;

public class ServerThread extends Thread {
	private Server server;
	private Socket socket;
	private boolean closed;

	public ServerThread(Server server, Socket socket) {
		this.server = server;
		this.socket = socket;

		start();
	}

	public void run() {
		try {
			DataInputStream input = new DataInputStream(socket.getInputStream());
			closed = false;
			while(!closed){
				String message = input.readUTF();
				
				System.out.println("Sending message to receiver.");
				
				server.sendToReceiver(message,socket);
				
				closed = socket.isClosed();
			}
		} catch (IOException e) {
			System.out.println(e.getMessage());
		} finally{
			server.removeConnection(socket);
		}
		return;
	}
}
